home *** CD-ROM | disk | FTP | other *** search
/ Programmer Plus 2007 / Programmer-Plus-2007.iso / Programming / Report Writers / Crystal Repot 9.0 Full CD version / Setup.exe / SRC / HOARDDLL.ZIP / 3rdParty / hoard / libhoard-2.0.2 / mlog.cpp < prev    next >
Encoding:
C/C++ Source or Header  |  2000-04-02  |  4.8 KB  |  189 lines

  1. ///-*-C++-*-//////////////////////////////////////////////////////////////////
  2. //
  3. // Hoard: A Fast, Scalable, and Memory-Efficient Allocator
  4. //        for Shared-Memory Multiprocessors
  5. // Contact author: Emery Berger, http://www.cs.utexas.edu/users/emery
  6. //
  7. // Copyright (c) 1998-2000, The University of Texas at Austin.
  8. //
  9. // This library is free software; you can redistribute it and/or modify
  10. // it under the terms of the GNU Library General Public License as
  11. // published by the Free Software Foundation, http://www.fsf.org.
  12. //
  13. // This library is distributed in the hope that it will be useful, but
  14. // WITHOUT ANY WARRANTY; without even the implied warranty of
  15. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  16. // Library General Public License for more details.
  17. //
  18. //////////////////////////////////////////////////////////////////////////////
  19.  
  20. #include "log.h"
  21. #include "memstat.h"
  22.  
  23. #include <hash_map.h> // STL
  24. #include <iostream.h>
  25.  
  26. // Return ceil(log_2(num)).
  27. // num must be positive.
  28. static int lg (int num)
  29. {
  30.   assert (num > 0);
  31.   int power = 0;
  32.   int n = 1;
  33.   // Invariant: 2^power == n.
  34.   while (n < num) {
  35.     n <<= 1;
  36.     power++;
  37.   }
  38.   return power;
  39. }
  40.  
  41.  
  42. struct eqUL
  43. {
  44.   bool operator()(unsigned long s1, unsigned long s2) const
  45.   {
  46.     return s1 == s2;
  47.   }
  48. };
  49.  
  50.  
  51. int main (int argc,
  52.       char *argv[])
  53. {
  54.   const int SIZE_CLASSES = 28;
  55.  
  56.   int inUse = 0;
  57.   int maxInUse = 0;
  58.   int allocated = 0;
  59.   int maxAllocated = 0;
  60.   int inUseAtMaxAllocated = 0;
  61.  
  62.   int numberOfObjects = 0;
  63.   int totalObjectSize = 0;
  64.  
  65.   Log<MemoryRequest> l, m;
  66.   hash_map<unsigned long, int, hash<unsigned long>, eqUL> table;
  67.  
  68.   int histogram[SIZE_CLASSES];
  69.   int inUseHistogram[SIZE_CLASSES];
  70.  
  71.   for (int i = 0; i < SIZE_CLASSES; i++) {
  72.     histogram[i] = 0;
  73.     inUseHistogram[i] = 0;
  74.   }
  75.  
  76.   int NLOGS = 128;
  77.   int MULTIPLIER = 1;
  78.  
  79.   char l_filename[255];
  80.  
  81.   int log = NLOGS;
  82.  
  83.   if (argc > 1) {
  84.     strncpy (l_filename, argv[1], 255);
  85.   } else {
  86.     sprintf (l_filename, "log%d", log);
  87.   }
  88.  
  89.   l.open (l_filename, Log<MemoryRequest>::READ_ONLY);
  90.   int i = 0;
  91.  
  92.   double prevTime = 0.0;
  93.  
  94.   while (i < l.length()) {
  95.  
  96.     printf (".");
  97.  
  98.     double currTime = l[i].getTime();
  99.     assert (prevTime <= currTime);
  100.     prevTime = currTime;
  101.     int sz = l[i].getSize();
  102.     int lgSz;
  103.     unsigned long addr = l[i].getAddress();
  104.  
  105.     switch (l[i].getType()) {
  106.  
  107.     case MemoryRequest::MALLOC_OP:
  108.  
  109.       if (sz == 0) {
  110.     sz = sizeof(double);
  111.       }
  112.       inUse += sz * MULTIPLIER;
  113.       assert (inUse <= allocated);
  114.       if (inUse > maxInUse) {
  115.     maxInUse = inUse;
  116.       }
  117.       table[addr] = sz * MULTIPLIER;
  118.       numberOfObjects++;
  119.       totalObjectSize += sz;
  120.       lgSz = lg(sz);
  121.       histogram[lgSz]++;
  122.       inUseHistogram[lgSz]++;
  123.       break;
  124.  
  125.     case MemoryRequest::FREE_OP:
  126.  
  127.       if (table[addr] != 0) {
  128.     inUse -= table[addr] * MULTIPLIER;
  129.     if (lg(table[addr] / MULTIPLIER) != 0) {
  130.       inUseHistogram[lg(table[addr] / MULTIPLIER)]--;
  131.     }
  132.       } else {
  133.     assert(0);
  134.       }
  135.       break;
  136.  
  137.     case MemoryRequest::ALLOCATE_OP:
  138.       
  139.       allocated += l[i].getAllocated() * MULTIPLIER;
  140.       if (allocated > maxAllocated) {
  141.     maxAllocated = allocated;
  142.     inUseAtMaxAllocated = inUse;
  143.       }
  144.       break;
  145.  
  146.     case MemoryRequest::DEALLOCATE_OP:
  147.  
  148.       allocated -= l[i].getDeallocated() * MULTIPLIER;
  149.       break;
  150.  
  151.     default:
  152.       // We should never get here.
  153.       printf ("yow!\n");
  154.       assert (0);
  155.       exit (1);
  156.       // cout << "Doh!" << endl;
  157.       break;
  158.     }
  159.     // cout << "(in use = " << inUse << ")" << endl;
  160.     i++;
  161.   }
  162.  
  163.   l.close();
  164.  
  165. #if 0
  166.   for (int i = 0; i < SIZE_CLASSES; i++) {
  167.     cout << (1 << i) << ": " << histogram[i] << " (still allocated = " << inUseHistogram[i] << ") " << endl;
  168.   }
  169. #endif
  170.  
  171.   totalObjectSize = totalObjectSize / MULTIPLIER;
  172.   allocated = allocated / MULTIPLIER;
  173.   inUseAtMaxAllocated = inUseAtMaxAllocated / MULTIPLIER;
  174.   maxAllocated = maxAllocated / MULTIPLIER;
  175.   inUse = inUse / MULTIPLIER;
  176.   maxInUse = maxInUse / MULTIPLIER;
  177.   cout << "Total number of objects = " << numberOfObjects << endl;
  178.   cout << "Total object size = " << totalObjectSize << endl;
  179.   cout << "Average object size = " << (double) totalObjectSize / (double) numberOfObjects << endl;
  180.   cout << "Allocated = " << allocated << endl;
  181.   cout << "Max allocated = " << maxAllocated << endl;
  182.   cout << "Max in use = " << maxInUse << endl;
  183.   cout << "In use at max allocated = " << inUseAtMaxAllocated << endl;
  184.   cout.precision(10);
  185.   cout << "Fragmentation (measure 3) = " << (double) maxAllocated / (double) inUseAtMaxAllocated << endl;
  186.   cout << "'Real' fragmentation (measure 4) = " << (double) maxAllocated / (double) maxInUse << endl;
  187.   cout << "Still in use = " << inUse << endl;
  188. }
  189.